import { GET_LIST_DATA } from '../actions/actionTypes';

const initState = {
  list: [],
  filterData: null,
  page: 0,
  isend: false
};

const contentListReducer = (state = initState, action) => {
  switch (action.type) {
    case GET_LIST_DATA:
      return getListData(state, action);
    default:
      return state;
  }
}

/**
 * 获取页面数据
 * @param {*} state 
 * @param {*} action 
 * @returns 
 */
const getListData = (state, action) => {
  let listData = [];
  let filterData = action.filterData || state.filterData;
  let page = action.toFirstPage ? 0 : state.page;
  let isend = false;
  
  if (page === 0) {
    listData = action.obj.data.poilist;
  } else {
    listData = state.list.concat(action.obj.data.poilist);
  }
  page = page + 1;
  if (page > 3) {
    isend = true;
  }
  return { ...state, list: listData, filterData, page, isend };
}

export default contentListReducer;